const nodeMailer = require("nodemailer");

const handlebars = require("handlebars");
const emailConfig = require("./config/index");
const { template: emailTemplate } = require("./template/emailTemplate")

const transporter = nodeMailer.createTransport({
    host: emailConfig.host,
    port: emailConfig.port,
    secure: true,
    auth: {
        user: emailConfig.user,
        pass: emailConfig.pass
    }
})


const sendEmail = (data) => {


    const templateFunc = handlebars.compile(emailTemplate);
    const htmlStr = templateFunc({ code: data.code })

    const mailOptions = {
        from: emailConfig.user,
        to: data.toEmail,
        subject: "忘记密码-邮箱验证",
        html: htmlStr
    }

    return new Promise((resolve, reject) => {
        transporter.sendMail(mailOptions).then(results => {
            resolve(results)
        }, (error) => {
            reject(error)
        })
    })

}
module.exports = {
    sendEmail
}